using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Collections.Generic;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace TweakSP.Web.Application
{
    public partial class TweakSP_ViewLsts : LayoutsPageBase
    {
        protected SPGridView grdViewLsts;
        protected SPGridView grdViewPages;

        protected override void OnLoad(EventArgs e)
        {
            BuildMenuTemplates();
            BindListGrid();
            BindPageGrid();
        }


        private void BuildMenuTemplates()
        {
            string webUrl = SPControl.GetContextWeb(Context).Url;

            #region List Menu
            MenuTemplate listMenu = new MenuTemplate();
            listMenu.ID = "ListMenu";

            MenuItemTemplate listSettings = new MenuItemTemplate("List Settings", "/_layouts/images/actionssettings.gif");
            listSettings.ClientOnClickNavigateUrl = "listedit.aspx?List={%LISTID%}";
            listMenu.Controls.Add(listSettings);

            MenuSeparatorTemplate sepMenu = new MenuSeparatorTemplate();
            listMenu.Controls.Add(sepMenu);

            MenuItemTemplate poshContext = new MenuItemTemplate("Script Context to PowerShell", "/_layouts/images/ICWSF.GIF");
            poshContext.ClientOnClickScript = "window.open('TweakSP_GetPoSH.aspx?REQUESTMODE=List&LISTID=%LISTID%', 'PoSH', 'width=700,height=500,scrollbars=1');";
            listMenu.Controls.Add(poshContext);

            MenuItemTemplate listSchema = new MenuItemTemplate("View List Schema", "/_layouts/images/existinglocations.gif");
            listSchema.ClientOnClickScript = "window.open('TweakSP_GetListSchema.aspx?LISTID=%LISTID%', 'ListSchema', 'width=700,height=500,scrollbars=1');";
            listMenu.Controls.Add(listSchema);

            this.Controls.Add(listMenu);

            #endregion List Menu
        }


        private void AddFieldToGrid(SPGridView grid,
                                    string header,
                                    string dataField)
        {
            SPBoundField newField = new SPBoundField();
            newField.HeaderText = header;
            newField.DataField = dataField;
            grid.Columns.Add(newField);
        }

        private void AddListFieldToGrid(SPGridView grid)
        {
            SPMenuField listField = new SPMenuField();
            listField.MenuTemplateId = "ListMenu";
            listField.HeaderText = "List";
            listField.TextFields = "Title";
            listField.TokenNameAndValueFields = "SITE=ParentWebUrl,LISTID=ID";

            grid.Columns.Add(listField);
        }



        private void BindListGrid()
        {
            using (SPWeb web = SPContext.Current.Web)
            {

                AddListFieldToGrid(grdViewLsts);
                AddFieldToGrid(grdViewLsts, "GUID", "ID");
                AddFieldToGrid(grdViewLsts, "Base Type", "BaseType");
                AddFieldToGrid(grdViewLsts, "Base Template", "BaseTemplate");
                AddFieldToGrid(grdViewLsts, "Hidden", "Hidden");
                AddFieldToGrid(grdViewLsts, "Items", "ItemCount");

                grdViewLsts.DataSource = web.Lists;
                grdViewLsts.DataBind();
            }
        }


        private void BindPageGrid()
        {
            List<SPFile> pages = GetAllFiles();

            AddFieldToGrid(grdViewPages, "Url", "Url");
            AddFieldToGrid(grdViewPages, "Title", "Title");
            AddFieldToGrid(grdViewPages, "Status", "CustomizedPageStatus");

            grdViewPages.DataSource = pages;
            grdViewPages.DataBind();
        }

        private List<SPFile> GetAllFiles()
        {
            using (SPWeb web = SPContext.Current.Web)
            {
                List<SPFile> pages = new List<SPFile>();

                GetFiles(ref pages, web.RootFolder);

                return pages;
            }
        }

        private void GetFiles(ref List<SPFile> files, SPFolder folder)
        {
            foreach (SPFile file in folder.Files)
            {
                files.Add(file);
            }

            foreach (SPFolder subFolder in folder.SubFolders)
            {
                GetFiles(ref files, subFolder);
            }
        }
    }
}
